<?php
/*********
*   积分商城列表页
* 
*/
namespace Home\Controller;
use Home\Controller\HomeController;
class JifenGoodsController extends HomeController {
 	public function _initialize(){
 		parent::_initialize();
 		
 	}
 	
	//积分商品列表
	public function jifenIndex(){
		$page = I('page')?I('page'):null;
        $open_id = I('openid');
       if($open_id){
		if(empty($page)){
			$page = 1;
		}
		$limit = 10;
		$num=$limit*($page-1);
		//$pages = dealPage($page,$limit);
		
		$user=M('member');
		$jifen=$user->where(array('openid'=>$open_id))->field("jifen")->find();//用户可用积分
		$gwc=M('cart');
		$count=$gwc->where("open_id='{$open_id}'")->count();	//购物车商品数量	
		$ban=M('banner');
		$pic=$ban->select();
		$goods = M('goods');
		$list['goodsinfo']=$goods->where(array('is_sale'=>1,'is_limit'=>0))->limit($num,$limit)->select();//商品信息
            foreach ($list['goodsinfo'] as &$v) {
                $dawq['goods_id']=$v['goods_id'];
                $dawq['is_price_main']=1;
                $v['orders']= M('rob_goods_orders')->where(array('goods_id'=>$v['goods_id'],'is_price_main'=>1))->find();
                //查询多少人在拼抢
                if(time()<$v['start_time']){
                    $v['zhuangtai']=-5;//未开始
                }else{
                    $v['zhuangtai']=1;
                    $v['start_time']=time();
                }
                if(time()>$v['end_time']){
                    $v['zhuangtai']=-4;//已结束
                }
                $dat['goods_id']=$v['goods_id'];
                $v['ren']=M('rob_goods_orders')->where($dat)->Count();
                $v['attr']=M('goods_attr')->where($dawq)->find();
            }
		$list['picture']=$pic;//轮播图
		$list['keyongjifen']=$jifen['jifen'];
		$list['count']=$count;
		$this->ajaxReturn($list);
    }else{
        $this->ajaxReturn(['msg'=>'openid 没有','code'=>-1]);
    }
		
	}
     public function jifenInfo(){
        $goods_id =I('goods_id');
        $goods = M('Goods')->where(array('goods_id' => $goods_id, 'is_sale' => 1, 'is_limit' => 0))->find();
        foreach ($goods as $k => $v) {
            
        }
     }
 
     public function goodsInfo() {
        $goods_id =I('goods_id');

        $goods = M('Goods')->where(array('goods_id' => $goods_id, 'is_sale' => 1, 'is_limit' => 0))->find();
        $goods['pic'] = M('Goods_pic')->where(array('goods_id' => $goods_id))->select();
        $goods['attr'] = M('Goods_attr')->where(array('goods_id' => $goods_id))->group('attr_id')->select();

        foreach ($goods['attr'] as &$v) {
            $name = M('Attr')->where(array('attr_id' => $v['attr_id']))->find();
            $v['name'] = $name['name'];
            if (($v['price'] != null && $v['price'] != 0) || ($v['jifen'] != null && $v['jifen'] != 0)) {
                $v['attr'] = M('Goods_attr')->where(array('goods_id' => $goods_id, 'attr_id' => $v['attr_id']))->select();
            } else {
                $attr = M('Goods_attr')->where(array('goods_id' => $goods_id, 'attr_id' => $v['attr_id']))->find();
                foreach (explode(',', $attr['attr_val']) as &$s) {
                    $v['attr'][]['attr_val'] = $s;
                }
            }

    
        }
        $goods['kucun'] = M('Goods_attr')->where(array('goods_id' => $goods_id))->sum('attr_kucun');
        $price = M('Goods_attr')->where(array('goods_id' => $goods_id, 'is_price_main' => 1))->find();
        $goods['price'] = $price['price'];
        $goods['jifen'] = $price['jifen'];
        $goods['fanjifen'] = $price['fanjifen'];
        $year = date('y', time());
        $mouth = date('m', time());
        $month_count = M('Count_month')->where(array('goods_id' => $goods_id, 'year' => $year, 'month' => $mouth))->find();
        if (!$month_count) {
            $data = [
                'goods_id' => $goods_id,
                'count' => 0,
                'year' => $year,
                'month' => $mouth,
            ];
            M('Count_month')->add($data);
            $goods['month_count'] = 0;
        } else {
            $goods['month_count'] = $month_count['count'];
        }

        $goods['content_all'] = html_entity_decode($goods['content_all']);
        $a_array = explode('<', $goods['content_all']);
        foreach ($a_array as $k => &$v) {
            $v = substr_replace($v, 'http://sc-wscjf.91quyum.com', 9, 0);
            if ($k != 0) {
                $array_last[] = $v;
            }
        }

        $this->ajaxReturn($goods);
    }
   public function  jifenGuize(){
        $guize=M('rob_rule');
        $list=$guize->where('type=1')->find();
        $this->ajaxReturn($list['content']);
    }


    public function config_changyong($guize){
        $j=M('config');
        $j=$j->select();
        foreach($j as $k =>$v){
            if($v['key']==$guize){
                $zz=$v['value'];
            }

        }
       return $zz;
    }
}
